package com.xmwzw.catpu.util;

import android.content.Context;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.xmwzw.catpu.R;


public class GlideImageLoader {
    public volatile static GlideImageLoader imageLoader;

    public static GlideImageLoader getInstance() {
        if (imageLoader == null) {
            synchronized (GlideImageLoader.class) {
                if (imageLoader == null) {
                    imageLoader = new GlideImageLoader();
                }
            }
        }
        return imageLoader;
    }

    public RequestOptions getRequestOptions() {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.centerCrop()
                .priority(Priority.HIGH)
                .placeholder(R.mipmap.ic_default_avart)
                .error(R.mipmap.ic_default_avart)
                .dontAnimate();
        return requestOptions;
    }

    public RequestOptions getRequestOptions(int errorRes, int holderRes) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.centerCrop()
                .priority(Priority.HIGH)
                .placeholder(holderRes)
                .error(errorRes)
                .dontAnimate();
        return requestOptions;
    }

    /*一般的图片设置*/
    public void setImage(Context context, Object object, ImageView imageView, RequestOptions requestOptions) {
        RequestBuilder requestBuilder = Glide.with(context).load(object).thumbnail(0.5f);
        if (requestOptions != null) {
            requestBuilder.apply(requestOptions);
        }
        requestBuilder.listener(new RequestListener() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object o, Target target, boolean b) {
                return false;
            }

            @Override
            public boolean onResourceReady(Object o, Object o2, Target target, DataSource dataSource, boolean b) {
                return false;
            }
        });
        requestBuilder.into(imageView);
    }


    /*times表示 h/w*/
    public RequestOptions getCompressOptions(int placeHolder, int error, double size, double times) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(placeHolder);
        requestOptions.error(error);
        int params[] = GlideImageLoader.getInstance().compress(size, times);
        requestOptions.override(params[0], params[1]);
        requestOptions.dontAnimate();
        return requestOptions;
    }

    /*设置图片大小*/
    public void setImage(Context context, int width, int height, Object object, ImageView imageView) {
        RequestOptions options = getRequestOptions();
        options.override(width, height);
        setImage(context, object, imageView, options);
    }





    /*一般的图片设置 使用默认的options*/
    public void setLocalImage(Context context, String object, ImageView imageView, RequestOptions options) {

        RequestBuilder requestBuilder = Glide.with(context).load(object).thumbnail(0.5f);
        requestBuilder.apply(options);
        //不做缓存
        options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE);
        requestBuilder.listener(new RequestListener() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object o, Target target, boolean b) {
                return false;
            }

            @Override
            public boolean onResourceReady(Object o, Object o2, Target target, DataSource dataSource, boolean b) {
                return false;
            }
        });
        requestBuilder.into(imageView);
    }


    /**
     * 设置图片是否缓存
     *
     * @param context
     * @param object
     * @param imageView
     * @param isNotCache 是否要缓存
     */
    public void setImageNocache(Context context, Object object, ImageView imageView, boolean isNotCache) {
        RequestOptions options = getRequestOptions();

        if (isNotCache) {
            //不做缓存
            options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE);
        }
        setImage(context, object, imageView, options);
    }

    /*设置centerCrop缩放模式*/
    public void setImageCenterCrop(Context context, Object object, ImageView imageView) {
        RequestOptions options = getRequestOptions();
        options.centerCrop();
        setImage(context, object, imageView, options);
    }

    /*设置fitCenter的缩放模式*/
    public void setImageFitCenter(Context context, Object object, ImageView imageView) {

        RequestOptions options = getRequestOptions();
        options.fitCenter();
        setImage(context, object, imageView, options);
    }

    /*设置圆形头像*/
    public void setRoundImage(Context context, Object object, ImageView imageView) {
        RequestOptions options = getRequestOptions();
        options.circleCrop();
        options.override(200, 200);
        setImage(context, object, imageView, options);
    }

    /*设置圆角图片*/
    public void setRoundCornerImage(Context context, Object object, ImageView imageView, int corner) {
        RequestOptions options = getRequestOptions();
        options.transform(new RoundedCorners(corner));
        options.override(200, 200);
        setImage(context, object, imageView, options);
    }

    /*显示gif图*/
    public void setGifImage(Context context, Object object, ImageView imageView) {

        Glide.with(context).asGif().load(object).into(imageView);

    }


    /*GLIDE默认采用RGB_565 2字节*/

    /**
     * @param size  要压缩的大小 M为单位
     * @param times H/W的比率
     * @return 返回w
     */
    public int[] compress(double size, double times) {
        int params[] = new int[2];
        double _size = size * 1024 * 1024 / 2;
        params[0] = (int) Math.sqrt(_size / times);
        params[1] = (int) (times * params[0]);
        return params;
    }

}